import { createRouter, createWebHistory } from 'vue-router'

// 路由配置
const routes = [
  {
    path: '/',
    redirect: '/preview'
  },
  {
    path: '/preview',
    name: 'Preview',
    component: () => import('../views/Preview.vue'),
    meta: { title: '文件预览' }
  },
  {
    path: '/file-reader',
    name: 'FileReader',
    component: () => import('../views/FileReader.vue'),
    meta: { title: '文件阅读器', requiresAuth: true },
    props: (route) => ({
      fileId: route.query.id,
      shareData: route.query.share
    })
  },
  {
    path: '/files/:path(.*)',
    name: 'FileBrowser',
    component: () => import('../views/Preview.vue'),
    props: (route) => {
      console.log('FileBrowser路由参数:', route.params);
      return { 
        initialPath: route.params.path || '',
        activeTab: 'files'
      };
    },
    meta: { 
      title: '文件浏览器',
      requiresAuth: true
    }
  },
  {
    path: '/admin-login',
    name: 'AdminLogin',
    component: () => import('../components/admin/components/AdminLogin.vue'),
    meta: { 
      title: '管理员登录'
    }
  },
  {
    path: '/admin',
    component: () => import('../views/Admin.vue'),
    children: [
      {
        path: 'settings',
        name: 'AdminSettings',
        component: () => import('../components/admin/index.vue'),
        meta: { 
          title: '管理员设置',
          requiresAuth: true,
          requiresAdmin: true
        }
      }
    ]
  },
  {
    path: '/admin-settings',
    redirect: '/admin/settings'
  },
  {
    path: '/cos-test',
    name: 'CosTest',
    component: () => import('../components/common/CosTest.vue'),
    meta: {
      title: 'COS服务测试',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  // 新增直接预览路由，支持通过URL快速预览文件
  {
    path: '/direct-preview',
    name: 'DirectPreview',
    component: () => import('../views/Preview.vue'),
    meta: {
      title: '文件直接预览',
      directPreview: true
    }
  },
  // 短链接预览路由
  {
    path: '/s/:code',
    name: 'ShortLinkPreview',
    component: () => import('../views/Preview.vue'),
    props: (route) => {
      console.log('短链接路由参数:', route.params);
      return { 
        shortCode: route.params.code || '',
        isShortLink: true
      };
    },
    meta: {
      title: '文件预览',
      isShortLink: true
    }
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/404.vue'),
    meta: { title: '404' }
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫，处理页面标题和权限
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 权限验证逻辑可以在这里处理
  // 例如：if (to.meta.requiresAuth && !isAuthenticated) { ... }
  
  next()
})

export default router 